//
//  SJFriendCell.m
//  WSL
//
//  Created by CJ on 2017/10/19.
//  Copyright © 2017年 sanjuetech. All rights reserved.
//

#import "SJFriendCell.h"
#import "SJUserItem.h"

@interface SJFriendCell ()

@property (strong, nonatomic) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *avatarImage;
@property (nonatomic, strong) UIView *bgView;

@end

@implementation SJFriendCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    [self.contentView addSubview:self.bgView];
    self.bgView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
    [self.contentView addSubview:self.avatarImage];
    self.avatarImage.sd_layout
    .centerYEqualToView(self.contentView)
    .leftSpaceToView(self.contentView, SJLayoutFrame(18))
    .heightIs(SJLayoutFrame(66))
    .widthIs(SJLayoutFrame(66));
    
    [self.contentView addSubview:self.nameLabel];
    self.nameLabel.sd_layout
    .leftSpaceToView(self.avatarImage, SJLayoutFrame(24))
    .centerYEqualToView(self.contentView)
    .widthIs(SJLayoutFrame(124))
    .autoHeightRatio(0);;
}


- (void)setItem:(SJUserItem *)item {
    
    _item = item;
    self.nameLabel.text = item.name;
    
    NSString *urlString = item.avatar;
    [_avatarImage setImageWithURL:[NSURL URLWithString:urlString] placeholder:[UIImage imageNamed:@"01_btn_delete"] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation completion:nil];
}

#pragma mark - getter

- (UILabel *)nameLabel{
    
    if (!_nameLabel) {
        
        _nameLabel = [UILabel new];
        _nameLabel.textColor = SJColorHexString(@"#ffffff");
        _nameLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(17)];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.numberOfLines = 0;
    }
    return _nameLabel;
}

- (UIView *)bgView {
    
    if (!_bgView) {
        
        _bgView = [UIView new];
        _bgView.sd_cornerRadius = @(SJLayoutFrame(6));
        _bgView.alpha = .6;
        _bgView.backgroundColor = SJColorHexString(@"#ffffff26");
    }
    return _bgView;
}

- (UIImageView *)avatarImage {
    
    if (!_avatarImage) {
        _avatarImage = [UIImageView new];
        _avatarImage.contentMode = UIViewContentModeScaleAspectFit;
        _avatarImage.clipsToBounds  = YES;
        _avatarImage.sd_cornerRadiusFromHeightRatio = @(0.5);
    }
    return _avatarImage;
}

@end
